package org.desktop.web.client.custom.widget;

import org.desktop.web.client.DesktopViewer;
import org.desktop.web.client.custom.form.LoginForm;
import org.desktop.web.shared.util.WebService;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.safehtml.shared.SafeUri;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.FramedPanel;
import com.sencha.gxt.widget.core.client.Window;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.BorderLayoutContainer;
import com.sencha.gxt.widget.core.client.container.BoxLayoutContainer.BoxLayoutPack;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SubmitCompleteEvent;
import com.sencha.gxt.widget.core.client.event.SubmitEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.event.SubmitCompleteEvent.HasSubmitCompleteHandlers;
import com.sencha.gxt.widget.core.client.event.SubmitCompleteEvent.SubmitCompleteHandler;
import com.sencha.gxt.widget.core.client.event.SubmitEvent.HasSubmitHandlers;
import com.sencha.gxt.widget.core.client.event.SubmitEvent.SubmitHandler;
import com.sencha.gxt.widget.core.client.form.FieldLabel;
import com.sencha.gxt.widget.core.client.form.FormPanel;
import com.sencha.gxt.widget.core.client.form.FormPanel.Method;
import com.sencha.gxt.widget.core.client.form.PasswordField;
import com.sencha.gxt.widget.core.client.form.TextField;

public class LoginWidget extends Window {

	ContentPanel panel=new ContentPanel();
	private DesktopViewer desktopViewer;

	public LoginWidget(DesktopViewer desktopViewer) {
		// TODO Auto-generated constructor stub
		this.desktopViewer = desktopViewer;
		setWidth(300);
		setHeight(200);
		setHeaderVisible(true);
		setHeadingText("Login");
		setClosable(false);
//		setModal(true);
		this.add(getWidget());
		show();

	}

	@Override
	public Widget getWidget() {
			panel.setHeaderVisible(false);
			panel.setButtonAlign(BoxLayoutPack.CENTER);
			final LoginForm form = new LoginForm(desktopViewer);
			form.setId("login");
			// form.setAction(GWT.getModuleBaseURL()+"exec?service="+WebService.AUTH);
			// form.setMethod(Method.POST);
			panel.add(form);

			VerticalLayoutContainer vlc = new VerticalLayoutContainer();
			form.add(vlc);

			TextField username = new TextField();
			username.setAllowBlank(false);
			username.setName("username");
			username.setId("username");
			vlc.add(new FieldLabel(username, "Username"),
					new VerticalLayoutData(-18, -1));

			PasswordField pwd = new PasswordField();
			pwd.setAllowBlank(false);
			pwd.setName("password");
			pwd.setId("password");
			vlc.add(new FieldLabel(pwd, "Password"), new VerticalLayoutData(
					-18, -1));

			TextButton btn = new TextButton("Reset");
			btn.addSelectHandler(new SelectHandler() {

				@Override
				public void onSelect(SelectEvent event) {
					form.reset();
					// TODO needs to be part of form panel, ie a Field interface
				}
			});

			panel.addButton(btn);

			btn = new TextButton("Submit");
			btn.addSelectHandler(new SelectHandler() {

				@Override
				public void onSelect(SelectEvent event) {
					if (!form.isValid()) {
						return;
					}

					// normally would submit the form but for example no server
					// set up to
					// handle the post
					// panel.submit();

					form.submit();

				}
			});
			panel.addButton(btn);

		// TODO Auto-generated method stub
//		addResizeHandler(new ResizeHandler() {
//			
//			@Override
//			public void onResize(ResizeEvent event) {
//				center();
//				
//			}
//		});
		return panel;
	}

}
